<?php

/**
 *
 * @category   Zend
 * @package    Core_Validate
 * @copyright  Copyright (c) 2008 Adam Kuśmierz
 */


/**
 * @see Zend_Validate_Abstract
 */
require_once 'Zend/Validate/Abstract.php';

/**
 * Class checks UUID value
 *
 * @category   Zend
 * @package    Core_Validate
 * @copyright  Copyright (c) 2008 Adam Kuśmierz
 */
class Core_Validate_Uuid extends Zend_Validate_Abstract
{
	/**
	 * Validation failure message key for when the value isn't UUID
	 */
	const NOT_UUID = 'notUuid';

	/**
	 * Validation failure message key for when the value is an empty string
	 */
	const STRING_EMPTY = 'stringEmpty';

	/**
	 * Validation failure message template definitions
	 *
	 * @var array
	 */
	protected $_messageTemplates = array(
	self::NOT_UUID   => "'%value%' is not valid UUID string",
	self::STRING_EMPTY => "'%value%' is an empty string"
	);

	/**
	 * Defined by Zend_Validate_Interface
	 *
	 * Returns true if and only if $value only contains proper UUID value
	 *
	 * @param  string $value
	 * @return boolean
	 */
	public function isValid($value)
	{
		$valueString = (string) $value;

		$this->_setValue($valueString);

		if ('' === $valueString) {
			$this->_error(self::STRING_EMPTY);
			return false;
		}
		
		if (!preg_match('/^[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}$/', $valueString)) {
			$this->_error(self::NOT_UUID);
			return false;
		}

		return true;
	}

}
